using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


using Material.Entity;
using Material.Object;

namespace Material.App
{
    public partial class FrmPenjBarangHRD : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();

        private TblTransPenjBarangDTL tblTransPenjBarangDTL;
        private TblTransPenjBarangDTLEntity tblTransPenjBarangDTLEntity;

        private TblTransPenjBarangHDR tblTransPenjBarangHDR;
        private TblTransPenjBarangHDREntity tblTransPenjBarangHDREntity;


        public static int idHDR = 0;

        public static string IdTransHDR = string.Empty;

        public FrmPenjBarangHRD()
        {
            InitializeComponent();
            EnabledButtons(true, true, true, true);
            ClearTextbox();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            
            dataGridView1.Rows.Add();

            dateJual.Value = DateTime.Now;

            lblPembelian.Text = "";
            btnHapus.Enabled = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnAddDetail, bool blbtnSave,bool btnFind)
        {
            ////btnNew.Enabled = blbtnNew;
            //btnAddDetail.Enabled = blbtnAddDetail;
            //btnDelete.Enabled = blbtnSave;
            //btnFindBarang.Enabled = btnFind;
            ////btnPrint.Enabled = blbtnPrint;

        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            ////txtKelompokBarang.Text = string.Empty;
            ////txtSatuanBarang.Text = string.Empty;
            ////txtNamaBarang.Text = string.Empty;
            //textBox1.Text = string.Empty;
        }


        private void btnFindBarang_Click(object sender, EventArgs e)
        {
            //FrmFindBarang frm = new FrmFindBarang();
            //frm.ShowDialog();
            //if ( frm.idBarang.Trim().Length > 0)
            //{
            //    tblBarangEntity = new TblBarangEntity(connStr);
            //    tblBarang = new TblBarang();
            //    tblBarang.Id = Convert.ToInt32(frm.idBarang);
            //    idBarang = Convert.ToInt32(frm.idBarang);
            //    tblBarang = tblBarangEntity.GetDataByID(tblBarang);

            //    //txtNamaBarang.Text = tblBarang.NamaBarang;
            //    //txtKelompokBarang.Text = tblBarang.TblKelompok.Kelompok;
            //    //txtSatuanBarang.Text = tblBarang.TblSatuan.Satuan;

            //    tblTransPembBarangHDR = new TblTransPembBarangHDR();
            //    tblTransPembBarangHDR.IdBarang = idBarang;
            //    tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);

            //    tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetDataByIDBarang(tblTransPembBarangHDR);
            //    idTrans = tblTransPembBarangHDR.IdTrans;
            //    FrmPembBarangDTL.idTransHDR = idTrans;
            //    FrmPembBarangDTL.idBarang = idBarang;
            //    BindGrid();
            //}
        }

        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            
            //FrmPembBarangDTL.stateInput = "Add";
            //if (dataGridView1.Rows.Count == 0)
            //    FrmPembBarangDTL.isHeader = true;
            //else
            //    FrmPembBarangDTL.isHeader = false;

            //FrmPembBarangDTL frm = new FrmPembBarangDTL();
            //frm.ShowDialog();
            //BindGrid();

            //if (DateTime.Now.ToString("dd/MM/yyyy").Equals(dateTimePicker1.Value.ToString("dd/MM/yyyy")))
            //{
            //    FrmPenjBarangDTL frm = new FrmPenjBarangDTL();
            //    FrmPenjBarangDTL.tglJual = dateTimePicker1.Value;
            //    if (dataGridView1.Rows.Count == 0)
            //        FrmPenjBarangDTL.InsertHDR = true;
            //    else
            //        FrmPenjBarangDTL.InsertHDR = false;

            //    FrmPenjBarangDTL.InsertDTL = true;
            //    FrmPenjBarangDTL.IdTransHDR = textBox1.Text.Trim();
            //    frm.ShowDialog();

            //    BindGrid();
            //    if (dataGridView1.Rows.Count > 0)
            //        btnCancel.Text = "Simpan";
            //}
            //else
            //    MessageBox.Show("Maaf Tambah Detail Penjualan Tidak Bisa Ditambah, Tanggal Sekarang "+DateTime.Now.ToString("dd/MM/yyyy")+"  Sudah Lewat Dari Tanggal Penjualan "+dateTimePicker1.Value.ToString("dd/MM/yyyy"), "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);

            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //int i = 0;
            //tblTransPembBarangHDR = new TblTransPembBarangHDR();
            //tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);
            //tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
            //tblTransPembBarangHDR.IdTrans = tblTransPembBarangHDR.IdTrans; 
            //tblTransPembBarangHDR.IdBarang = idBarang;
            //tblTransPembBarangHDR.Created = DateTime.Now;
            //tblTransPembBarangHDR.CreatedBy = "admin";
            //tblTransPembBarangHDR.Modified=DateTime.Now;
            //tblTransPembBarangHDR.ModifiedBy="admin";

            //i = tblTransPembBarangHDREntity.Insert(tblTransPembBarangHDR);

            //if (i != -1)
            //{
            //    MessageBox.Show("sukses");
            //    btnAddDetail.Enabled = true;
            //    btnFindBarang.Enabled = false;
            //    btnSave.Enabled = false;
            //    //frmPembBarangDTL = new FrmPembBarangDTL();
            //    FrmPembBarangDTL.idTransHDR = tblTransPembBarangHDR.IdTrans;

                
            //}
            //else
            //    MessageBox.Show("tida");
            ////tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
            ////idTrans = tblTransPembBarangHDR.IdTrans;
        }

        

        //private void BindGrid()
        //{
        //    tblTransPemBarangDTL = new TblTransPemBarangDTL();
        //    tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);

        //    tblTransPemBarangDTL.IdTransHDR = idTrans;
        //    dataGridView1.AutoGenerateColumns = false;
        //    dataGridView1.DataSource = tblTransPemBarangDTLEntity.GetDataByIDTransHDR(tblTransPemBarangDTL);
        //}

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //tblTransPemBarangDTL = new TblTransPemBarangDTL();
            //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 1:
                        FrmPenjBarangDTL.TglJual = dateJual.Value;
                        FrmPenjBarangDTL frm = new FrmPenjBarangDTL();
                        frm.ShowDialog();
                        BindGrid();
                        break;
                    case 9:
                        tblTransPenjBarangDTL = new TblTransPenjBarangDTL();
                        tblTransPenjBarangDTLEntity = new TblTransPenjBarangDTLEntity(connStr);
                        if (dataGridView1.Rows[e.RowIndex].Cells[2].Value!=DBNull.Value)
                        {
                            DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                            if (dr == DialogResult.Yes)
                            {
                                //ambil id barang dari Penjualan Barang Detail
                                tblTransPenjBarangDTL = tblTransPenjBarangDTLEntity.GetDataByID(Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()));

                                //Get Ambil data Barang dari Trans Penjualan DTL
                                TblBarang brg = new TblBarang();
                                TblBarangEntity brgEnt = new TblBarangEntity(connStr);
                                brg.Id = tblTransPenjBarangDTL.IdBarang;
                                brg = brgEnt.GetDataByID(brg);
                                brg.ModifiedBy = FrmLogin.UserID;
                                brg.ModifiedDate = DateTime.Now;
                                brg.Jumlah = brg.Jumlah + tblTransPenjBarangDTL.Jumlah;
                                //update jumlah barang yang sudah dihapus
                                brgEnt.UpdateJmlBarang(brg);

                                 //hapus Id Trans Penjualan Detail
                                 tblTransPenjBarangDTLEntity.Delete(Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()));
                                 

                            }
                            BindGrid();
                        }

                        break;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
             //int i = 0;
             //DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
             //if (dr == DialogResult.Yes)
             //{
             //    List<TblTransPenjBarangDTL> lst = new List<TblTransPenjBarangDTL>();
             //    tblTransPenjBarangDTL =  new TblTransPenjBarangDTL();
             //    tblTransPenjBarangDTLEntity = new TblTransPenjBarangDTLEntity(connStr);
             //    tblTransPenjBarangDTL.IdTransHDR = textBox1.Text;
             //    lst = tblTransPenjBarangDTLEntity.GetDataAllByIdTransHDR(tblTransPenjBarangDTL);

             //    if (lst.Count > 0)
             //    {
             //        foreach (TblTransPenjBarangDTL data in lst)
             //        {
             //            tblTransPemBarangDTL = new TblTransPemBarangDTL();
             //            tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
             //            tblTransPemBarangDTL.IdTransDTL = data.IdBarang;
             //            tblTransPemBarangDTL = tblTransPemBarangDTLEntity.GetDataByID(tblTransPemBarangDTL);

             //            tblTransPemBarangDTL.IdTransDTL = tblTransPemBarangDTL.IdTransDTL;
             //            tblTransPemBarangDTL.Jumlah = tblTransPemBarangDTL.Jumlah + data.Jumlah;// Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()); ;
             //            tblTransPemBarangDTL.Modified = DateTime.Now;
             //            tblTransPemBarangDTL.ModifiedBy = FrmLogin.UserID;
             //            tblTransPemBarangDTL.TotalHarga = tblTransPemBarangDTL.Harga1 * tblTransPemBarangDTL.Jumlah;// Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()); ;

             //            tblTransPemBarangDTLEntity.UpdateJumlah(tblTransPemBarangDTL);
             //        }
             //    }
             //    i = tblTransPenjBarangDTLEntity.DeleteByIdTransHDR(tblTransPenjBarangDTL);
             //    if (i != -1)
             //    {
             //        tblTransPenjBarangHDR = new TblTransPenjBarangHDR();
             //        tblTransPenjBarangHDREntity = new TblTransPenjBarangHDREntity(connStr);

             //        tblTransPenjBarangHDR.IdTransHDR = textBox1.Text;
             //        i = tblTransPenjBarangHDREntity.Delete(tblTransPenjBarangHDR);
             //        idTrans = 0;
             //    }

             //    if (i != -1)
             //    {
             //        MessageBox.Show("Data berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
             //        ClearTextbox();
             //        BindGrid();
             //    }
             //    else
             //        MessageBox.Show("Data tidak berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

             //}


             //string a = string.Empty;
           
            


        }

        private void BindGrid()
        {
            tblTransPenjBarangDTL = new TblTransPenjBarangDTL();
            tblTransPenjBarangDTLEntity = new TblTransPenjBarangDTLEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblTransPenjBarangDTLEntity.GetDataByIdTransHDR(idHDR).Tables[0];

            this.dataGridView1.AllowUserToAddRows = true;
            decimal total = 0;
            lblPembelian.Text = "";
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                total += Convert.ToDecimal(dataGridView1.Rows[i].Cells[7].Value.ToString());
            }

            if(dataGridView1.Rows.Count-1>0)
                lblPembelian.Text = " Total Pembelian : Rp." + total.ToString("###,###,###.00"); //String.Format("Order Total: {0:N}", total.ToString());
           
 
          
           
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        //    tblTransPenjBarangHDR = new TblTransPenjBarangHDR();
        //    tblTransPenjBarangHDREntity = new TblTransPenjBarangHDREntity(connStr);

        //    tblTransPenjBarangHDR = tblTransPenjBarangHDREntity.GetDataByMaxID();
        //    textBox1.Text = tblTransPenjBarangHDR.IdTransHDR;
        //    FrmPenjBarangDTL.IdTransHDR = textBox1.Text;

        //    dateTimePicker1.Enabled = true;
        //    btnAddDetail.Enabled = true;
        //    btnFind.Enabled = false;
        //    btnNew.Enabled = false;
        //    btnCancel.Enabled = true;
        //    btnDelete.Enabled = false;
        //    dateTimePicker1.Value = DateTime.Now;

        //}

        //private void btnCancel_Click(object sender, EventArgs e)
        //{
        //    dateTimePicker1.Enabled = false;
        //    btnNew.Enabled = true;
        //    btnFind.Enabled = false;
        //    btnCancel.Enabled = false;
        //    btnAddDetail.Enabled = false;
        //    textBox1.Text = string.Empty;
        //    btnCancel.Text = "Batal";
        //    BindGrid();
        //    btnFind.Enabled = true;
        //}

        //private void btnFind_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        FrmFindPenjBarang frm = new FrmFindPenjBarang();
        //        frm.ShowDialog();
        //        textBox1.Text = IdTransHDR;
        //        BindGrid();
        //        btnAddDetail.Enabled = true;
        //        tblTransPenjBarangHDR = new TblTransPenjBarangHDR();
        //        tblTransPenjBarangHDREntity = new TblTransPenjBarangHDREntity(connStr);
        //        tblTransPenjBarangHDR.IdTransHDR = textBox1.Text.Trim();
        //        tblTransPenjBarangHDR = tblTransPenjBarangHDREntity.GetDataByID(tblTransPenjBarangHDR);
        //        dateTimePicker1.Value = Convert.ToDateTime(tblTransPenjBarangHDR.TglJual);
        //        btnDelete.Enabled = true;
        //        //dateTimePicker1.Enabled = true;
        //        //FrmPenjBarangDTL.InsertHDR = false;
        //    }
        //    catch
        //    {
        //    }
        }

        private void btnCetak_Click(object sender, EventArgs e)
        {

            FrmRptPenjBarang frm = new FrmRptPenjBarang();
            FrmRptPenjBarang.IdHDR = idHDR;
            frm.ShowDialog();
        }

        private void FrmPenjBarangHRD_Load(object sender, EventArgs e)
        {

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            dateJual.Enabled = true;
            btnBrowse.Enabled = false;
            btnNew.Enabled = false;
            btnHapus.Enabled = false;
            dataGridView1.DataSource = null;
           // dataGridView1.Rows.Add();
            btnBatal.Enabled = true;
            tblTransPenjBarangHDREntity = new TblTransPenjBarangHDREntity(connStr);
           
            tblTransPenjBarangHDR = new TblTransPenjBarangHDR();
            tblTransPenjBarangHDR.TglJual = dateJual.Value;
            tblTransPenjBarangHDR.CreatedBy = FrmLogin.UserID;
            tblTransPenjBarangHDR.CreatedDate = DateTime.Now;

            tblTransPenjBarangHDREntity.Insert(tblTransPenjBarangHDR);
            idHDR = tblTransPenjBarangHDREntity.GetDataMaxID();



        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FrmFindPenjBarang frm = new FrmFindPenjBarang();
            frm.ShowDialog();
            BindGrid();
            btnHapus.Enabled = true;
            btnNew.Enabled = false;
            btnBatal.Enabled = true;


        }

        private void btnHapus_Click(object sender, EventArgs e)
        {
            tblTransPenjBarangHDR = new TblTransPenjBarangHDR();
            tblTransPenjBarangHDREntity = new TblTransPenjBarangHDREntity(connStr);
            tblTransPenjBarangHDREntity.Delete(Convert.ToInt32(idHDR));
            if (tblTransPenjBarangHDREntity.Delete(Convert.ToInt32(idHDR)) != -1)
            {
               

                //MessageBox.Show("Data berhasil di hapus.", "Pesan", MessageBoxButtons.OK);

                //BindGrid();


                tblTransPenjBarangDTL = new TblTransPenjBarangDTL();
                tblTransPenjBarangDTLEntity = new TblTransPenjBarangDTLEntity(connStr);

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    //ambil id barang dari Penjualan Barang Detail
                    tblTransPenjBarangDTL = tblTransPenjBarangDTLEntity.GetDataByID(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString()));

                    //Get Ambil data Barang dari Trans Penjualan DTL
                    TblBarang brg = new TblBarang();
                    TblBarangEntity brgEnt = new TblBarangEntity(connStr);
                    brg.Id = tblTransPenjBarangDTL.IdBarang;
                    brg = brgEnt.GetDataByID(brg);
                    brg.ModifiedBy = FrmLogin.UserID;
                    brg.ModifiedDate = DateTime.Now;
                    brg.Jumlah = brg.Jumlah + tblTransPenjBarangDTL.Jumlah;
                    //update jumlah barang yang sudah dihapus
                    brgEnt.UpdateJmlBarang(brg);

                    //hapus Id Trans Penjualan Detail
                    tblTransPenjBarangDTLEntity.Delete(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString()));
                }

                BindGrid();
                MessageBox.Show("Data berhasil di hapus.", "Pesan", MessageBoxButtons.OK);

               
            }

        
            
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            btnNew.Enabled = true;
            btnBrowse.Enabled = true;
            dateJual.Enabled = false;
            btnBatal.Enabled = false;
            idHDR = 0;
            BindGrid();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }


    }
}